<template>
  <div>
    <van-nav-bar
      title="修改密码"
      left-text="返回"
      left-arrow
      :fixed="true"
      @click-left="onClickLeft"
      @click-right="onClickRight"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="确认密码"
        label="确认密码"
        placeholder="确认密码"
        :rules="[{ required: true, message: '请再次确认密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit" @click="changepass">提交</van-button>
      </div>
    </van-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      username: "",
      password: ""
    };
  },
  methods: {
    onSubmit(values) {
      console.log("submit", values);
    },

    onClickLeft() {
        this.$router.go(-1);
    },

    changepass: function() {
        if (this.username == this.password) {
          this.$axios
            .put("/updateinspector", {
              password: this.password,
              id: localStorage.getItem("insceptorid")
            })
            .then(res => {
              if (res.data.code == 200) {
                alert("修改成功");
                this.$router.push({ path: "/" });
              } else {
                alert("修改失败请重新尝试");
              }
            })
            .error(error => {
              alert("修改失败请重新尝试");
            });
        } else {
          alert("两次输入密码不一致");
        }
      }
  }
};
</script>

